/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.monkey;

import com.intellij.ide.util.PsiElementListCellRenderer;
import com.intellij.psi.*;
import com.intellij.psi.presentation.java.ClassPresentationUtil;

import javax.swing.*;

// Referenced classes of package com.intellij.ide.util:
//            PsiElementListCellRenderer, PsiClassListCellRenderer

public class ReferenceCellRenderer extends PsiElementListCellRenderer {

    protected Icon getIcon(PsiElement psielement) {
        return getContainingMember(psielement).getIcon(0);
    }

    public String getElementText(PsiElement psiElement) {
        PsiMember member = getContainingMember(psiElement);
        if (member == null) {
            return psiElement.getText();
        }
        PsiNamedElement psinamedelement = getContainingElement(member);
        return ((psinamedelement instanceof PsiClass) ? ClassPresentationUtil.getNameForClass((PsiClass) psinamedelement, false) : psinamedelement.getName()) + "." + getContainingMember(psiElement).getName();
    }

    public String getContainerText(PsiElement psielement, String s) {
        com.intellij.psi.PsiFile psifile = psielement.getContainingFile();
        if (psifile instanceof PsiJavaFile) {
            PsiJavaFile psijavafile = (PsiJavaFile) psifile;
            String s1 = psijavafile.getPackageName();
            if (s1.length() == 0)
                return null;
            else
                return "(" + s1 + ")";
        } else {
            return null;
        }
    }

    public int getIconFlags() {
        return 2;
    }

    private PsiMember getContainingMember(PsiElement psielement) {
        while (psielement != null && (!(psielement instanceof PsiMember))) {
            psielement = psielement.getParent();
        }
        System.out.println("psielement = " + psielement);
        return (PsiMember) psielement;
    }

    private static PsiNamedElement getContainingElement(PsiMember psimethod) {
        PsiClass psiclass = psimethod.getContainingClass();
        if (psiclass == null)
            return psimethod.getContainingFile();
        else
            return psiclass;
    }
}
